﻿using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using Newtonsoft.Json;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Security.Cryptography;
using System.Collections.Specialized;

namespace QDPay.Bus.util
{
    public class RestHttp
    {
        public static string Get(string url)
        {
            var request = (HttpWebRequest)WebRequest.Create(url);
            return Get(url, request);
        }

        public static T Get<T>(string url)
        {
            var request = (HttpWebRequest)WebRequest.Create(url);
            var wwwresult = Get(url, request);
            if (string.IsNullOrEmpty(wwwresult))
                return default(T);
            return JsonConvert.DeserializeObject<T>(wwwresult);
        }

        private static string Get(string url, HttpWebRequest request)
        {
            Trace.TraceInformation(url);
            request.Method = "GET";
            var response = (HttpWebResponse)request.GetResponse();
            var resStream = response.GetResponseStream();
            if (resStream == null) return string.Empty;
            var wwwresult = new StreamReader(resStream).ReadToEnd();
            Trace.TraceInformation(wwwresult);
            return wwwresult;
        }

        public static string Request(string url)
        {
            Trace.TraceInformation(url);

            var request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            var response = (HttpWebResponse)request.GetResponse();
            var resStream = response.GetResponseStream();
            if (resStream == null) return string.Empty;
            var wwwresult = new StreamReader(resStream).ReadToEnd();
            Trace.TraceInformation(wwwresult);
            return wwwresult;
        }
        public static T Post<T>(string url, string data)
        {
            Trace.TraceInformation(url);
            Trace.TraceInformation(data);
            var request = (HttpWebRequest)WebRequest.Create(url);
            var postData = Encoding.UTF8.GetBytes(data);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = postData.Length;
            using (var stream = request.GetRequestStream())
            {
                stream.Write(postData, 0, postData.Length);
            }
            var response = (HttpWebResponse)request.GetResponse();
            var resStream = response.GetResponseStream();
            if (resStream == null) return default(T);
            var wwwresult = new StreamReader(resStream).ReadToEnd();
            Trace.TraceInformation(wwwresult);
            return JsonConvert.DeserializeObject<T>(wwwresult);
        }

        public static string Post(string url, string data)
        {
            return Post(url, data, "application/x-www-form-urlencoded");
        }
        public static string JsonPost(string url, string data)
        {
            return Post(url, data, "application/json");
        }

        private static string Post(string url, string data,string contentType)
        {
            Trace.TraceInformation(url);
            Trace.TraceInformation(data);
            var request = (HttpWebRequest)WebRequest.Create(url);
            var postData = Encoding.UTF8.GetBytes(data);
            request.Method = "POST";
            request.ContentType = contentType;
            request.ContentLength = postData.Length;
            using (var stream = request.GetRequestStream())
            {
                stream.Write(postData, 0, postData.Length);
            }
            var response = (HttpWebResponse)request.GetResponse();
            var resStream = response.GetResponseStream();
            if (resStream == null) return string.Empty;
            var wwwresult = new StreamReader(resStream).ReadToEnd();
            Trace.TraceInformation(wwwresult);
            return wwwresult;
        }
        

        public static string GetMapPath(string path)
        {
            return HttpContext.Current != null ? HttpContext.Current.Server.MapPath(path) : System.Web.Hosting.HostingEnvironment.MapPath(path);
        }
    }
}